package com.yihu.wlyy.web.doctor.team;

import com.yihu.wlyy.entity.doctor.team.admin.AdminTeam;
import com.yihu.wlyy.entity.doctor.team.admin.AdminTeamMember;
import com.yihu.wlyy.service.app.account.DoctorInfoService;
import com.yihu.wlyy.service.app.team.DrAdminTeamService;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(value = "/doctor/admin-teams", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "医生行政管理团队")
public class DrAdminTeamController extends BaseController {
    @Autowired
    private DoctorInfoService doctorInfoService;

    @Autowired
    private DrAdminTeamService teamService;

    @Autowired
    private DrAdminTeamService memberService;

    @ResponseBody
    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "团队数量")
    public String teamCount() {
        try {
            long count = teamService.getTeamCount();

            JSONObject json = new JSONObject();
            json.put("count", count);
            return write(200, "OK", "data", json);
        } catch (Exception e) {
            error(e);
            return error(-1, e.getMessage());
        }
    }

    /*@ResponseBody
    @RequestMapping(method = RequestMethod.GET)
    @ApiOperation(value = "团队列表")
    public String getTeamList(
            @RequestParam("filter") String filter,
            @RequestParam("order") String order,
            @RequestParam("page") int page,
            @RequestParam("size") int pageSize) {
        return null;
    }*/

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    @ApiOperation(value = "创建团队")
    public String createTeam(@RequestParam("team_name") String teamName,
                             @RequestParam("org_code") String orgCode) {
        try {
            AdminTeam team = teamService.createTeam(teamName, orgCode);

            return write(200, "OK", "data", team);
        } catch (Exception e) {
            error(e);
            return error(-1, e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ApiOperation(value = "更新团队")
    public String updateTeam(@PathVariable("id") long teamId,
                             @RequestParam("team_name") String teamName,
                             @RequestParam("org_code") String orgCode) {
        try {
            AdminTeam team = teamService.updateTeam(teamId, teamName, orgCode);

            return write(200, "OK", "data", team);
        } catch (Exception e) {
            error(e);
            return error(-1, e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "团队信息")
    public String getTeam(@PathVariable("id") long teamId) {
        try {
            AdminTeam team = teamService.getTeam(teamId);

            return write(200, "OK", "data", team);
        } catch (Exception e) {
            error(e);
            return error(-1, e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/{teamId}/members", method = RequestMethod.POST)
    @ApiOperation(value = "添加成员")
    public String addMember(@PathVariable("teamId") long teamId,
                          @RequestParam("doctor_id") long doctorId,
                          @RequestParam("is_leader") boolean isLeader,
                          @RequestParam("role") String role) {
        try {
            teamService.addMember(teamId, doctorId, isLeader, role);

            return write(200, "OK", "data", "");
        } catch (Exception e) {
            error(e);
            return error(-1, e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/members", method = RequestMethod.GET)
    @ApiOperation(value = "团队成员")
    public String getTeamMembers(@PathVariable("id") long teamId) {
        try {
            List<AdminTeamMember> members = memberService.getMembers(teamId);

            return write(200, "OK", "data", new JSONArray(members));
        } catch (Exception e) {
            error(e);
            return error(-1, e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/{team_id}/members/{doctor_id}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    @ApiOperation(value = "移除成员")
    public String removeMember(@PathVariable("team_id") long teamId,
                             @PathVariable("doctor_id") long doctorId) {

        try {
            memberService.removeMember(teamId, doctorId);

            return write(200, "OK", "data", "");
        } catch (Exception e) {
            error(e);
            return error(-1, e.getMessage());
        }
    }
}
